package com.zj.leetcode.easy.hash;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * @program: algorithm
 * @description:
 * Intersection of Two Arrays
 * 349 intersection-of-two-arrays
 * @author: Zhang Bo
 * @create: 2022-01-14 17:05
 **/
public class IntersectionOfTwoArrays{
    public static void main(String[] args) {
        Solution solution = new IntersectionOfTwoArrays().new Solution();
    }
    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public int[] intersection(int[] nums1, int[] nums2) {

            ArrayList<Integer> list = new ArrayList<>();
            Map<Integer, Integer> map = new HashMap<Integer, Integer>();
            for (int i = 0; i < nums1.length; i++) {
                map.put(nums1[i], 0);
            }

            for (int i = 0; i < nums2.length; i++) {
                Integer orDefault = map.getOrDefault(nums2[i], -1);
                if(orDefault == 0){
                    list.add(nums2[i]);
                    map.put(nums2[i], ++orDefault);
                }
            }
            return list.stream().mapToInt(Integer::valueOf).toArray();

        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}
